
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sultan
 */
public class Scheculer extends TimerTask {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    public Scheculer() {
        conn = DataBaseConect.ConnectDb();
    }

    public void startTimer(){
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(this, 0, 15000000);
     }
    
    public static void main(String args[]) {
       new Scheculer().startTimer();
    }

    @Override
    public void run() {
        //System.out.println("This is a 4.1 Hour Timer.%n");
        //System.out.println("Desingn by Sultan%n");
        
        try {
            String sql = "select * from lifeSpane where Flag='NotProcessed'";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                String dateOfMaturity= rs.getString("DateOfMiturity");
                SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date mDate = DateFormat.parse(dateOfMaturity);
                //System.out.println("DateOfMiturity"+ mDate);
                java.util.Date todaysDate = Calendar.getInstance().getTime();
                //System.out.println("Todays date "+todaysDate);
                int cmprResult =todaysDate.compareTo(mDate);
                //System.out.println("Compare result "+ cmprResult);
                String toDaysString = DateFormat.format(todaysDate);
                if(cmprResult >= 0){
                   String GetId = rs.getString("ID");
                   String GetQuantity = rs.getString("Quantity");
                   String GetDetail=rs.getString("Detail");
                   String sql1 = "insert into journal (lifeSpaneID,detail,quantity,Date,type) values (?,?,?,?,?)";
                   pst=conn.prepareStatement(sql1);
                   pst.setString(1, GetId);
                   pst.setString(2, GetDetail);
                   pst.setString(3, GetQuantity);
                   pst.setString(4, toDaysString);
                   pst.setString(5, "Production");
                   pst.execute();
                   String sql2 ="update lifeSpane set Flag='Processed' where ID=?";
                   pst=conn.prepareStatement(sql2);
                   pst.setString(1, GetId);
                   pst.execute();
                   //System.out.println("All the quirries has been Executed");
               }
               }
               }catch (Exception e) {
               e.printStackTrace();
               JOptionPane.showMessageDialog(null, e);
               }finally {
               try {
               rs.close();
               pst.close();
               } catch (Exception e) {
               e.printStackTrace();
               }
               }
    
    }
}
